package com.yh.csx.bsf.flow.dao.dal;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.List;

import com.xxl.job.core.util.DateUtil;
import com.yh.csx.bsf.core.base.Ref;
import com.yh.csx.bsf.core.util.StringUtils;
import com.yh.csx.bsf.flow.provider.enums.TaskStateEnum;
import com.yh.csx.bsf.flow.core.db.ConvertUtil;
import com.yh.csx.bsf.flow.core.db.DbConn;
import com.yh.csx.bsf.flow.dao.dal.auto.t_task_base_dal;
import com.yh.csx.bsf.flow.dao.model.auto.t_task_model;
import lombok.val;

/** 代码自动生成工具自动生成,不要在这里写自己的代码，否则会被自动覆盖哦 - 车毅*/
public class t_task_dal extends t_task_base_dal
{
    public Integer max(DbConn conn)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select max(id) as maxid from t_task s");
        List<Map<String,Object>> ds = conn.executeList(stringSql.toString(), Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            return ConvertUtil.objToInt(ds.get(0).get("maxid"));
        }
        return null;
    }
    public List<t_task_model> getPage(DbConn db,Integer flowid,Integer taskid,String title, String approve_node,String approve_userno, String approve_by, String create_by, Integer task_state, String create_time_from,String create_time_to, Integer pageindex, Integer pagesize,Ref<Integer> totalSize){
        val par = new ArrayList<>();
        val rs = new ArrayList<t_task_model>();

        StringBuilder sb = new StringBuilder(" from t_task where 1=1 ");
        if(flowid!=null&&flowid>=0){
            par.add(flowid);
            sb.append(" and flow_id=?");
        }
        if(taskid!=null&&taskid>=0){
            par.add(taskid);
            sb.append(" and id=?");
        }
        if(!StringUtils.isEmpty(title)){
            par.add(title);
            sb.append(" and flow_title like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(approve_node)){
            par.add(approve_node);
            sb.append(" and flow_approve_node like concat('%', ?, '%')");
        }

        if(!StringUtils.isEmpty(approve_userno)){
            par.add(approve_userno);
            sb.append(" and flow_approve_userno like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(approve_by)){
            par.add(approve_by);
            sb.append(" and flow_approve_by like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(create_by)){
            par.add(create_by);
            sb.append(" and create_by like concat('%', ?, '%')");
        }
        if(task_state!=null&&task_state>=0){
            par.add(task_state);
            sb.append(" and task_state=?");
        }
        if(!StringUtils.isEmpty(create_time_from)){
            par.add(DateUtil.parse(create_time_from,"yyyy-MM-dd HH:mm:ss"));
            sb.append(" and create_time_from > ?");
        }
        if(!StringUtils.isEmpty(create_time_to)){
            par.add(DateUtil.parse(create_time_to,"yyyy-MM-dd HH:mm:ss"));
            sb.append(" and create_time_to < ?");
        }

        String sql = "select * "+sb.toString() +" order by id desc "+String.format(" limit %s,%s",(pageindex-1)*pagesize,pagesize);
        String countSql = "select count(0) "+sb.toString();
        val ds = db.executeList(sql, par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        totalSize.setData(ConvertUtil.objToInt(db.executeScalar(countSql,par.toArray())));
        return rs;
    }

    public Integer add2(DbConn conn, t_task_model model){
        add(conn,model);
        return ConvertUtil.objToInt(conn.executeScalar("select @@IDENTITY;",null));
    }
//    public int getCountbyFlowIdWithState(DbConn conn, int flowid, TaskStateEnum taskStateEnum)
//    {
//        ArrayList<Object> Par = new ArrayList<Object>();
//        Par.add(flowid);
//        Par.add(taskStateEnum.Value);
//        StringBuilder stringSql = new StringBuilder();
//        stringSql.append("select count(0) from t_task s where s.flowid=? and s.task_state=?");
//        int count = ConvertUtil.objToInt(conn.executeScalar(stringSql.toString(), Par.toArray()));
//        return count;
//    }

    public List<Integer> getTimeoutList(DbConn conn){
        ArrayList<Integer> rs = new ArrayList<>();
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(TaskStateEnum.waiting.Value);
        List<Map<String,Object>> ds = conn.executeList("select id from t_task t where t.timeout>0 and t.task_state=? and t.timeout_time<now()",  Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(ConvertUtil.objToInt(dr.get("id")));
            }
        }
        return rs;
    }

    public List<String> getResults(DbConn conn, int flowid,String flow_approve_node,long batch_id)
    {
        ArrayList<String> rs = new ArrayList<>();
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid);
        Par.add(batch_id);
        Par.add(TaskStateEnum.done.Value);
        Par.add(flow_approve_node);

        List<Map<String,Object>> ds = conn.executeList("select task_result from t_task t where t.flow_id=? and t.batch_id=? and t.task_state=? and t.flow_approve_node=?",  Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(ConvertUtil.objToStr(dr.get("task_result")));
            }
        }
        return rs;

    }

    public List<t_task_model> getTasks(DbConn conn, int flowid,int task_state)
    {
        ArrayList<t_task_model> rs = new ArrayList<>();
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid);
        Par.add(task_state);

        List<Map<String,Object>> ds = conn.executeList("select t.* from t_task t where t.flow_id=? and t.task_state=?",  Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        return rs;

    }
    public t_task_model getCurrentTask(DbConn conn, int flowid)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid); 

        List<Map<String,Object>> ds = conn.executeList("select t.* from t_task t  where id=(select max(id) from t_task a where  a.flow_id=?)",  Par.toArray());
        if (ds != null && ds.size() > 0)
        {
        	return createModel(ds.get(0));            
        }
        return null;
    }


    public Integer getCountOfTasks(DbConn conn, int flowid,int task_state)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid);
        Par.add(task_state);

        return ConvertUtil.objToInt( conn.executeScalar("select count(0) from t_task t where t.flow_id=? and t.task_state=?",  Par.toArray()));
    }

    public List<t_task_model> getTasks(DbConn conn, int flowid)
    {
        ArrayList<t_task_model> rs = new ArrayList<>();
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid);

        List<Map<String,Object>> ds = conn.executeList("select t.* from t_task t where t.flow_id=?",  Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        return rs;
    }

    public boolean deleteTasks(DbConn conn, int flowid)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flowid);

        String Sql = "delete from t_task where flow_id=?";
        int rev = conn.executeSql(Sql, Par.toArray());
        if (rev == 1)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    public boolean closedAllTasks(DbConn conn, int flowid,String userby)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(TaskStateEnum.close.Value);
        Par.add(userby);
        Par.add(flowid);
        Par.add(TaskStateEnum.waiting.Value);
        int rev = conn.executeSql("update t_task set task_state=?,update_time=now(),update_by=? where flow_id=? and task_state=?",  Par.toArray());
        return rev == 1;
    }

    public boolean closedAllTasks(DbConn conn, int flowid,String flow_approve_node,long batch_id, String userby)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(TaskStateEnum.close.Value);
        Par.add(userby);
        Par.add(batch_id);
        Par.add(flowid);
        Par.add(TaskStateEnum.waiting.Value);
        Par.add(flow_approve_node);
        int rev = conn.executeSql("update t_task set task_state=?,update_time=now(),update_by=?,batch_id=? where flow_id=? and task_state=? and flow_approve_node=?",  Par.toArray());
        return rev == 1;
    }

    public boolean updateTaskState(DbConn conn, int taskid,String userby,int taskState)
    {
        ArrayList<Object> Par = new ArrayList<Object>();

        Par.add(userby);
        Par.add(taskState);
        Par.add(taskid);
        int rev = conn.executeSql("update t_task set update_time=now(),update_by=?,task_state=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateTaskResult(DbConn conn, int taskid,String userby,String taskResult)
    {
        ArrayList<Object> Par = new ArrayList<Object>();

        Par.add(userby);
        Par.add(taskResult);
        Par.add(taskid);
        int rev = conn.executeSql("update t_task set update_time=now(),update_by=?,task_result=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean transferTask(DbConn conn, int taskid,String flow_approve_userno,String flow_approve_userby)
    {
        ArrayList<Object> Par = new ArrayList<Object>();

        Par.add(flow_approve_userby);
        Par.add(flow_approve_userno);
        Par.add(flow_approve_userby);
        Par.add(taskid);
        int rev = conn.executeSql("update t_task set update_time=now(),update_by=?,flow_approve_userno=?,flow_approve_by=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateTaskTimeout(DbConn conn, int taskid,int timeout,Date timeout_time)
    {
        ArrayList<Object> Par = new ArrayList<Object>();

        Par.add(timeout);
        Par.add(timeout_time);
        Par.add(taskid);
        int rev = conn.executeSql("update t_task set update_time=now(),timeout=?,timeout_time=? where id=?",  Par.toArray());
        return rev == 1;

    }
}
